#!/usr/bin/env bash

#################################################################################
# Setup
#################################################################################

REMOTE_TASKS_DIR="${MISE_CACHE_DIR}/remote-git-tasks-cache"
MISE_V2025117_CACHE_DIR="${REMOTE_TASKS_DIR}/791bacd406835a949d44cc2ac1106e0e4475d131ccc56b2540846d661bf5ff5c"

cargo init --name hello_cargo

#################################################################################
# Test remote tasks with no ref
#################################################################################

cat <<EOF >mise.toml
[tasks.remote_lint_https_latest]
file  = "git::https://github.com/jdx/mise.git//xtasks/lint/ripgrep"
EOF

assert_contains "mise tasks" "remote_lint_https_latest"
assert_succeed "mise run remote_lint_https_latest" # Remote task should be downloaded
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

assert_succeed "MISE_TASK_REMOTE_NO_CACHE=true mise run remote_lint_https_latest" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_https_latest --no-cache" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_https_latest" # Cache should be used
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

#################################################################################
# Test remote tasks with with ref
#################################################################################

cat <<EOF >mise.toml
[tasks.remote_lint_https_ref]
file  = "git::https://github.com/jdx/mise.git//xtasks/lint/ripgrep?ref=v2025.1.17"
EOF

assert_contains "mise tasks" "remote_lint_https_ref"
assert_succeed "mise run remote_lint_https_ref" # Remote task should be downloaded
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_exists "${MISE_V2025117_CACHE_DIR}"
assert_directory_not_empty "${MISE_V2025117_CACHE_DIR}"

mise cache clear # Clear cache to force redownload

assert_succeed "MISE_TASK_REMOTE_NO_CACHE=true mise run remote_lint_https_ref" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_https_ref --no-cache" # Remote task should be redownloaded
assert_directory_not_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_exists "${MISE_V2025117_CACHE_DIR}"

assert_succeed "mise run remote_lint_https_ref" # Cache should be used
assert_directory_exists "${REMOTE_TASKS_DIR}"
assert_directory_not_empty "${REMOTE_TASKS_DIR}"
assert_directory_exists "${MISE_V2025117_CACHE_DIR}"
assert_directory_not_empty "${MISE_V2025117_CACHE_DIR}"
